//
//  BGTenderAndAuctionCell.m
//  bigMall
//
//  Created by LTOVE on 16/8/15.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGTenderAndAuctionCell.h"

#import "BGTenderAuctionModel.h"
#import "LTOVELabelEdgInsert.h"
#import "MJExtension.h"
#define lineMagin 5
static NSString *const reuseridentify = @"cell";
@implementation BGTenderAndAuctionCell

- (void)setModel:(BGTenderAuctionModel *)model
{
    [self SetUpAllSubViewsWith:model];
    
}



- (void)SetUpAllSubViewsWith:(BGTenderAuctionModel *)model
{
//    NSMutableArray *contentArray = [[NSMutableArray alloc]initWithArray:self.contentView.subviews];
//    [contentArray removeAllObjects];
    
    for (UILabel *label in self.contentView.subviews) {
        [label removeFromSuperview];
    }
    
    for (UIImageView *imgV in self.contentView.subviews) {
        [imgV removeFromSuperview];
    }
    CGFloat tempWith = 2 * BGMaginLeft + 70 + 5;
    
    tempWith = model.iScaiFen ? tempWith + 48 + 5 :tempWith;
    if ([model.guaDanFangShi isEqualToString:@"信誉挂单"]|| [model.guaDanFangShi isEqualToString:@"仓单挂单"]) {
        tempWith += 5 + 60.5;
    }else{
        tempWith += 5 + 73.5;
    }
//    NSArray *a = self.contentView.subviews;
    //分割线
    NSLog(@"%f±±±±±",tempWith);
    UILabel *lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, screenW, BGMaginButtom)];
    lineLabel.backgroundColor = dockwhite_;
    [self.contentView addSubview:lineLabel];
    //标题
    UILabel *titleLabel = [[UILabel alloc]init];
    
//    GWLog(@"%@",model.keyValues);
    titleLabel.text = model.title;
    titleLabel.font = Font34;
    titleLabel.textColor = textBlackColor;
    [titleLabel sizeToFit];
    if (titleLabel.width > screenW - tempWith) {
        titleLabel.size = CGSizeMake(screenW - tempWith, 16);
    }
    titleLabel.x = BGMaginLeft;
    titleLabel.y = BGMaginTop + CGRectGetMaxY(lineLabel.frame);
    [self.contentView addSubview:titleLabel];
    //拆分
    
    LTOVELabelEdgInsert *CFLabel = [[LTOVELabelEdgInsert alloc]init];
    CFLabel.text = @"可拼单";
    CFLabel.textColor = lightwhite_;
    CFLabel.font = Font30;
    [CFLabel sizeToFit];
    CFLabel.contentInsets = UIEdgeInsetsMake(0, 5, 0, 5);
    CFLabel.hidden = model.iScaiFen;
    CFLabel.x = CGRectGetMaxX(titleLabel.frame) + 5;
    CFLabel.centerY = titleLabel.centerY;
    CFLabel.backgroundColor = blue_;
    CFLabel.layer.cornerRadius = 5;
    CFLabel.layer.masksToBounds = YES;
    CFLabel.hidden = !model.iScaiFen;
    [self.contentView addSubview:CFLabel];
    //挂单方式
    LTOVELabelEdgInsert *GDLabel = [[LTOVELabelEdgInsert alloc]init];
    GDLabel.text = model.guaDanFangShi;
//    NSLog(@"%@",model.guaDanFangShi);
//    GDLabel.text = @"信誉挂单";
    if ([model.guaDanFangShi isEqualToString:@"信誉挂单"]) {
        
        GDLabel.backgroundColor = GWColor(251, 159, 52, 1);
    }else if ([model.guaDanFangShi isEqualToString:@"保证金挂单"]){
        GDLabel.backgroundColor = GWColor(55, 209, 205, 1);
    }else{
        GDLabel.backgroundColor = GWColor(249, 101, 52, 1);
    }
    GDLabel.textColor = lightwhite_;
    GDLabel.font = Font30;
    [GDLabel sizeToFit];
    GDLabel.contentInsets = UIEdgeInsetsMake(0, 5, 0, 5);
    GDLabel.centerY = titleLabel.centerY;
//    GDLabel.backgroundColor = green_;
    if (CFLabel.hidden) {
        GDLabel.x = CFLabel.x;
        
    }else{
        GDLabel.x = CGRectGetMaxX(CFLabel.frame) + 5;
        
    }
    GDLabel.layer.cornerRadius = 5;
    GDLabel.layer.masksToBounds = YES;
    [self.contentView addSubview:GDLabel];
    //开始时间
    UILabel *timeLabel = [UILabel new];
    timeLabel.textColor = textGrayColor;
    timeLabel.text = model.time;
    timeLabel.font = Font30;
    [timeLabel sizeToFit];
    timeLabel.centerY = titleLabel.centerY;
    timeLabel.x = screenW - timeLabel.width - BGMaginRight;
    [self.contentView addSubview:timeLabel];
    //北方四港
    UILabel *BFSGLabel = [UILabel new];
    BFSGLabel.text = model.beiFangSiGang;
    BFSGLabel.font = Font34;
    BFSGLabel.textColor = textBlackColor;
    [BFSGLabel sizeToFit];
    BFSGLabel.x = BGMaginLeft;
    BFSGLabel.y = CGRectGetMaxY(titleLabel.frame) + lineMagin;
    [self.contentView addSubview:BFSGLabel];
    //价格
    NSDictionary *dic = @{
                          NSFontAttributeName:Font30,
                          NSForegroundColorAttributeName:textBlackColor,
                          };
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:model.price attributes:dic];
    NSRange rang = [model.price rangeOfString:@":"];
    [attrStr addAttribute:NSForegroundColorAttributeName value:read_ range:NSMakeRange(rang.location + 1, model.price.length - 1 - rang.location)];
    UILabel *priceLabel = [UILabel new];
    priceLabel.attributedText = attrStr;
    [priceLabel sizeToFit];
    priceLabel.x = BGMaginLeft;
    priceLabel.y = CGRectGetMaxY(BFSGLabel.frame) + lineMagin;
    [self.contentView addSubview:priceLabel];
    //分割线
    UILabel *miniLineLabel = [UILabel new];
    miniLineLabel.width = 1;
    miniLineLabel.height = priceLabel.height;
    miniLineLabel.x = CGRectGetMaxX(priceLabel.frame) + 5;
    miniLineLabel.centerY = priceLabel.centerY;
    miniLineLabel.backgroundColor = black_;
    [self.contentView addSubview:miniLineLabel];
    //数量
    UILabel *numLabel = [UILabel new];
    numLabel.text = model.Num;
    numLabel.font = Font30;
    numLabel.textColor = textBlackColor;
    [numLabel sizeToFit];
    numLabel.centerY = priceLabel.centerY;
    numLabel.x = CGRectGetMaxX(miniLineLabel.frame) + 5;
    [self.contentView addSubview:numLabel];
    //交货地  + 图片
    UIImage *image = [UIImage imageNamed:model.imageOne];
    UIImageView *imageV = [[UIImageView alloc]initWithImage:image];
    imageV.height = priceLabel.height;
    imageV.width = imageV.height;
    imageV.x = BGMaginLeft;
    imageV.y = CGRectGetMaxY(priceLabel.frame) + lineMagin;
    [self.contentView addSubview:imageV];
    UILabel *JFDLabel = [UILabel new];
    JFDLabel.textColor = textBlackColor;
    JFDLabel.text = model.Add;
//    JFDLabel.text = @"交割地:河南洛阳";
    JFDLabel.font = Font30;
//    JFDLabel.preferredMaxLayoutWidth = 20;
    JFDLabel.width = (screenW - 2 * BGMaginLeft) / 2.0;
    JFDLabel.height = 16;
//    [JFDLabel sizeToFit];
    JFDLabel.centerY = imageV.centerY;
    JFDLabel.x = CGRectGetMaxX(imageV.frame) + lineMagin;
    [self.contentView addSubview:JFDLabel];
    //截止时间
    UILabel *endTimeLabel = [UILabel new];
    endTimeLabel.text = model.jiaoHuoFangShi;
    endTimeLabel.font = Font30;
    endTimeLabel.textColor = textBlackColor;
    [endTimeLabel sizeToFit];
    endTimeLabel.centerY = imageV.centerY;
    endTimeLabel.x = screenW - endTimeLabel.width - BGMaginRight;
    [self.contentView addSubview:endTimeLabel];
    UIImage *imageTwo = [UIImage imageNamed:model.imageTwo];
    UIImageView *twoImageV = [[UIImageView alloc]initWithImage:imageTwo];
    twoImageV.size = imageV.size;
    twoImageV.centerY = imageV.centerY;
    twoImageV.x = endTimeLabel.x - lineMagin - twoImageV.width;
    [self.contentView addSubview:twoImageV];
    
}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    BGTenderAndAuctionCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseridentify];
    if (!cell) {
        cell = [[self alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseridentify];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

@end
